package com.demo.yuanzi.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.demo.yuanzi.entity.YuanziSaUser;
import com.demo.yuanzi.dao.YuanziSaUserDao;
import com.demo.yuanzi.entity.authDTO.YuanZiUserDTO;
import com.demo.yuanzi.service.YuanziSaUserService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * (YuanziSaUser)表服务实现类
 *
 * @author makejava
 * @since 2024-05-09 14:16:50
 */
@Service("yuanziSaUserService")
public class YuanziSaUserServiceImpl implements YuanziSaUserService {
    @Resource
    private YuanziSaUserDao yuanziSaUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YuanziSaUser queryById(Integer id) {
        return this.yuanziSaUserDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param yuanziSaUser 实例对象
     * @return 实例对象
     */
    @Override
    public YuanziSaUser insert(YuanziSaUser yuanziSaUser) {
        this.yuanziSaUserDao.insert(yuanziSaUser);
        return yuanziSaUser;
    }

    /**
     * 修改数据
     *
     * @param yuanziSaUser 实例对象
     * @return 实例对象
     */
    @Override
    public YuanziSaUser update(YuanziSaUser yuanziSaUser) {
        this.yuanziSaUserDao.update(yuanziSaUser);
        return this.queryById(yuanziSaUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.yuanziSaUserDao.deleteById(id) > 0;
    }

    @Override
    public List<String> getPermissionByUserId(Integer userId) {

        return this.yuanziSaUserDao.queryPermissionByUserId(userId);

    }


    @Override
    public List<YuanZiUserDTO> getRoleByUserId(Integer userId) {
        return this.yuanziSaUserDao.queryRolesByUserId(userId);
    }

    @Override
    public SaTokenInfo login(String username, Integer password) {
        YuanziSaUser user = this.yuanziSaUserDao.queryByName(username);
        StpUtil.login(user.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();



        return tokenInfo;
    }

}
